<?php

class OOWP_Query
    extends OOWP_Base
{

    public function __construct(WP_Query $query)
    {
        $this->query = $query;
    }

    /* ACCESSORS */

    protected function getPosts( )
    {
        return $this->query->posts;
    }

    protected function getQuery( )
    {
        return $this->dict['query'];
    }

    protected function setQuery(WP_Query $query)
    {
        $this->dict['query'] = $query;
    }

    /* METHODS */

    public function find_post_after(stdClass $post)
    {
        $filtered = array_filter($this->posts,
            function ($element) use ($post) {
                return ($element->ID == $post->ID);
            }
        );
        if (count($filtered) != 1) { return null; }
        $key = array_search(current($filtered), $this->posts);
        if (array_key_exists($key - 1, $this->posts)) {
            return $this->posts[$key - 1];
        }
        return null;
    }

    public function find_post_before(stdClass $post)
    {
        $filtered = array_filter($this->posts,
            function ($element) use ($post) {
                return ($element->ID == $post->ID);
            }
        );
        if (count($filtered) != 1) { return null; }
        $key = array_search(current($filtered), $this->posts);
        if (array_key_exists($key + 1, $this->posts)) {
            return $this->posts[$key + 1];
        }
        return null;
    }

    public function format_query( )
    {
        $result = '';
        $result .= '<pre>';
        ob_start();
        var_dump($this->query);
        $capture = ob_get_contents();
        ob_end_clean();
        $result .= htmlspecialchars($capture, ENT_QUOTES);
        $result .= '</pre>';
        return $result;
    }

}
